<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<%@ include file="../common.jsp"%>
<%@ page import="cn.firstsoft.firstframe.admin.PropertyFile"%>
<%@ page import="org.springside.core.Constants" %>
<%@ page import="java.io.*,java.util.*,org.jdom.*,org.jdom.input.*,org.jdom.output.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>管理控制台 - 环境设置 - 短信集成</title>
<%@ include file="/common/meta2.jsp"%>
<link href="../style.css" type="text/css" rel=stylesheet>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
	<table width="100%" align="center" >
		<TR>
			<TH class=listViewPaginationTdS1  align=left colSpan=4>
			设置手机短信结果
			</TH>
		</TR>	
		<tr>
			<td widht="100%">
<%
	//得到当前的短信厂商


	String providerClass = "";
	String provider = request.getParameter("provider");
	if (provider == null) provider = "";
	
	try {
		String xmlFile = System.getProperty("oa_application.config") + "/cn/firstsoft/modules/message/applicationContext-message-manager.xml";
		File file = new File(xmlFile);
		if (!file.canWrite()) {
			out.println("<br>文件为只读状态，请修改文件属性："+ xmlFile);
		}
		SAXBuilder builder = new SAXBuilder();
		Document doc;
		doc = builder.build(file);
		Element beans = doc.getRootElement();
		List beanList = beans.getChildren("bean");
		Element beanM = null;
		Element commonSMSServiceBean = null;
		for (int i=0; i<beanList.size(); i++) {
			Element bean = (Element)beanList.get(i);
			if ("commonSMSSender".equals(bean.getAttributeValue("name"))) {
				beanM = bean;
			}
			if("commonSMSService".equals(bean.getAttributeValue("id"))){
				commonSMSServiceBean = bean;
			}
		}
		if (!"".equals(provider)) {
			//从没有到有，需要把切面打开
			
			String cls = null;
			if(provider.equals("Xuanwu")){
				cls = "SMSService";
			}else{
				cls = "MsgSender";
			}
			
			//不存在就增加一个

			if(commonSMSServiceBean == null){
				Element provederBean = new Element("bean");
				provederBean.setAttribute("id", "commonSMSService");
				provederBean.setAttribute("class", "cn.firstsoft.firstframe.plugins.sms.impl."+provider + cls);
				beans.addContent(provederBean);
				
				Element property = new Element("property");
				property.setAttribute("name", "commonSMSService");
				property.setAttribute("ref", "commonSMSService");
				beanM.addContent(property);
			}else{
				//如果已经存在，则修改提供类

				commonSMSServiceBean.setAttribute("class", "cn.firstsoft.firstframe.plugins.sms.impl."+provider + cls);
			}
			
			
		} else {
			Element beanP = null;
			for (int i=0; i<beanList.size(); i++) {
				Element bean = (Element)beanList.get(i);
				if ("commonSMSService".equals(bean.getAttributeValue("id"))) {
					beanP = bean;
				}
			}
			if(beanP != null){
				beans.removeContent(beanP);
			}
			
			List propertyList = beanM.getChildren("property");
			for (int j=0; j<propertyList.size(); j++) {
				Element property = (Element)propertyList.get(j);
				if ("commonSMSService".equals(property.getAttributeValue("name"))) {
					beanM.removeContent(property);
				}
			}
		}
		XMLOutputter outp = new XMLOutputter();
		outp.setFormat(Format.getPrettyFormat());
		outp.output(doc, new FileOutputStream(file));	

		out.println("<br>完成修改文件：" + xmlFile);		
				
	} catch (JDOMException e) {
		out.println("有错误：" + e.getMessage());
		e.printStackTrace();
	} catch (IOException e) {
		out.println("有错误：" + e.getMessage());
		e.printStackTrace();
	} catch (Exception e) {
		out.println("有错误：" + e.getMessage());
		e.printStackTrace();
	}
	
	//修改firstframe.properties的参数


	if (!"".equals(provider)) {
		if (!"".equals(providerClass)) {
			//原来有，现在有则无变化


		} else {
			//原来无，现在有


			PropertyFile.writeProperties("firstframe.properties", "plugins.sms", "true");
			out.println("<br>完成修改文件：firstframe.properties");		
		}
	} else {
		//if (!"".equals(providerClass)) {
			PropertyFile.writeProperties("firstframe.properties", "plugins.sms", "false");
			out.println("<br>完成修改文件：firstframe.properties");		
		//} else {
		//}	
	}
	
	//修改具体的参数文件


	if ("Short".equals(provider)) {
		String port = request.getParameter("port");
		if (port == null) port = "COM1";
		String baudRate = request.getParameter("baudRate");
		if (baudRate == null) baudRate = "115200 ";
		String smsCNum = request.getParameter("smsCNum");
		if (smsCNum == null) smsCNum = "+8613800755500";
		
		PropertyFile.writeProperties("conf/gsmDeviceConnectionSettings.properties", "port", port);
		PropertyFile.writeProperties("conf/gsmDeviceConnectionSettings.properties", "baudRate", baudRate);
		PropertyFile.writeProperties("conf/gsmDeviceConnectionSettings.properties", "smsCNum", smsCNum);
	}

	if ("XuanWu".equals(provider) || "SouthSea".equals(provider) || "JassonSoft".equals(provider)) {
		String host = request.getParameter("host");
		if (host == null) host = "";
		String port = request.getParameter("port");
		if (port == null) port = "80";
		String username = request.getParameter("username");
		if (username == null) username = "";
		String password = request.getParameter("password");
		if (password == null) password = "";
		String serviceaddress = request.getParameter("serviceaddress");
		if (serviceaddress == null) serviceaddress = "";				
		String useProxy = request.getParameter("useProxy");
		if (useProxy == null) useProxy = "off";
		String proxyType = request.getParameter("proxyType");
		if (proxyType == null) proxyType = "http";				
		String proxyIP = request.getParameter("proxyIP");
		if (proxyIP == null) proxyIP = "192.168.0.1";
		String proxyPort = request.getParameter("proxyPort");
		if (proxyPort == null) proxyPort = "3128";				
		String proxyUser = request.getParameter("proxyUser");
		if (proxyUser == null) proxyUser = "mol";
		String proxyPass = request.getParameter("proxyPass");
		if (proxyPass == null) proxyPass = "mol";		
		
		try {
		
			String xmlFile = System.getProperty("oa_application.config") + "/conf/sendmsgconfig.xml";
			File file = new File(xmlFile);
			if (!file.canWrite()) {
				out.println("<br>文件为只读状态，请修改文件属性："+ xmlFile);
			}

			SAXBuilder builder = new SAXBuilder();
			Document doc;
			
			doc = builder.build(file);
			Element sendMsgConfig = doc.getRootElement();
			Element hostEle = sendMsgConfig.getChild("host");
			hostEle.setText(host);

			Element portEle = sendMsgConfig.getChild("port");
			portEle.setText(port);

			Element usernameEle = sendMsgConfig.getChild("username");
			usernameEle.setText(username);
			
			Element passwordEle = sendMsgConfig.getChild("password");
			passwordEle.setText(password);

			Element serviceaddressEle = sendMsgConfig.getChild("serviceaddress");
			serviceaddressEle.setText(serviceaddress);

			Element proxyserverinfo = sendMsgConfig.getChild("proxyserverinfo");

			Element useProxyEle = proxyserverinfo.getChild("useProxy");
			useProxyEle.setText(useProxy);

			Element proxyTypeEle = proxyserverinfo.getChild("proxyType");
			proxyTypeEle.setText(proxyType);

			Element proxyIPEle = proxyserverinfo.getChild("proxyIP");
			proxyIPEle.setText(proxyIP);

			Element proxyPortEle = proxyserverinfo.getChild("proxyPort");
			proxyPortEle.setText(proxyPort);
			
			Element proxyUserEle = proxyserverinfo.getChild("proxyUser");
			proxyUserEle.setText(proxyUser);	
			
			Element proxyPassEle = proxyserverinfo.getChild("proxyPass");
			proxyPassEle.setText(proxyPass);

			XMLOutputter outp = new XMLOutputter();
			Format format = Format.getPrettyFormat();
			format.setEncoding("gb2312");
			outp.setFormat(format);
			outp.output(doc, new FileOutputStream(file));	
					
			out.println("<br>完成短信设置！修改文件："+xmlFile);
					
		} catch (JDOMException e) {
			out.println("有错误：" + e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			out.println("有错误：" + e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			out.println("有错误：" + e.getMessage());
			e.printStackTrace();
		}					
	}
	
	if ("Ifly".equals(provider)) {
		String k_account = request.getParameter("k_account");
		if (k_account == null) k_account = "test";
		String k_password = request.getParameter("k_password");
		if (k_password == null) k_password = "test";
		String k_source = request.getParameter("k_source");
		if (k_source == null) k_source = "000";
		String k_host = request.getParameter("k_host");
		if (k_host == null) k_host = "auth.ifly.com.cn";
		String k_port = request.getParameter("k_port");
		if (k_port == null) k_port = "8390";	
					
		PropertyFile.writeProperties("conf/smsconfig.properties", "ifly.k_account", k_account);
		PropertyFile.writeProperties("conf/smsconfig.properties", "ifly.k_password", k_password);
		PropertyFile.writeProperties("conf/smsconfig.properties", "ifly.k_source", k_source);
		PropertyFile.writeProperties("conf/smsconfig.properties", "ifly.k_host", k_host);
		PropertyFile.writeProperties("conf/smsconfig.properties", "ifly.k_port", k_port);
		out.println("<br>完成金蝶IFly短信设置！");		
	}
	
	if ("Mobset".equals(provider)) {
		String strHost = request.getParameter("strHost");
		if (strHost == null) strHost = "www.mobset.com";
		String iCorpID = request.getParameter("iCorpID");
		if (iCorpID == null) iCorpID = "0000";
		String strLoginName = request.getParameter("strLoginName");
		if (strLoginName == null) strLoginName = "test";
		String strPasswd = request.getParameter("strPasswd");
		if (strPasswd == null) strPasswd = "test";
		String iTimeOut = request.getParameter("iTimeOut");
		if (iTimeOut == null) iTimeOut = "30";	
					
		PropertyFile.writeProperties("conf/smsconfig.properties", "mobset.strHost", strHost);
		PropertyFile.writeProperties("conf/smsconfig.properties", "mobset.iCorpID", iCorpID);
		PropertyFile.writeProperties("conf/smsconfig.properties", "mobset.strLoginName", strLoginName);
		PropertyFile.writeProperties("conf/smsconfig.properties", "mobset.strPasswd", strPasswd);
		PropertyFile.writeProperties("conf/smsconfig.properties", "mobset.iTimeOut", iTimeOut);

		out.println("<br>完成首易短信设置！");		
	}
	
	if ("Surge".equals(provider)) {
		String url = request.getParameter("url");
		if (url == null) url = "jdbc:jtds:sqlserver://localhost:1433;DatabaseName=sms_server";
		String driver = request.getParameter("driver");
		if (driver == null) driver = "net.sourceforge.jtds.jdbc.Driver";
		String dbname = request.getParameter("dbname");
		if (dbname == null) dbname = "sa";
		String dbpass = request.getParameter("dbpass");
		if (dbpass == null) dbpass = "";
		String epid = request.getParameter("epid");
		if (epid == null) epid = "xxxx";	
		String userid = request.getParameter("userid");
		if (userid == null) userid = "test";
		String pass = request.getParameter("pass");
		if (pass == null) pass = "";			
		String type = request.getParameter("type");
		if (type == null) type = "jdbc";	
							
		PropertyFile.writeProperties("conf/smsconfig.properties", "surge.jdbc.url", url);
		PropertyFile.writeProperties("conf/smsconfig.properties", "surge.jdbc.driver", driver);
		PropertyFile.writeProperties("conf/smsconfig.properties", "surge.jdbc.dbname", dbname);
		PropertyFile.writeProperties("conf/smsconfig.properties", "surge.jdbc.dbpass", dbpass);
		PropertyFile.writeProperties("conf/smsconfig.properties", "surge.epid", epid);
		PropertyFile.writeProperties("conf/smsconfig.properties", "surge.api.userid", userid);
		PropertyFile.writeProperties("conf/smsconfig.properties", "surge.api.pass", pass);
		PropertyFile.writeProperties("conf/smsconfig.properties", "surge.type", type);
		out.println("<br>完成巨澜短信设置！");
	}	
	
	if ("Weina".equals(provider)) {
		String weinaaccount = request.getParameter("weina.account");
		if (weinaaccount == null) weinaaccount = "wf0603";
		String weinapassword = request.getParameter("weina.password");
		if (weinapassword == null) weinapassword = "123456";
		String weinahost = request.getParameter("weina.host");
		if (weinahost == null) weinahost = "http://kingdee.weinaduo.net";
		String weinamethod = request.getParameter("weina.method");
		if (weinamethod == null) weinamethod = "/send/g70send.aspx";	
		String weinacharset = request.getParameter("weina.charset");
		if(weinacharset==null) weinacharset="GBK";
					
		PropertyFile.writeProperties("conf/smsconfig.properties", "weina.account", weinaaccount);
		PropertyFile.writeProperties("conf/smsconfig.properties", "weina.password", weinapassword);
		PropertyFile.writeProperties("conf/smsconfig.properties", "weina.host", weinahost);
		PropertyFile.writeProperties("conf/smsconfig.properties", "weina.method", weinamethod);
		PropertyFile.writeProperties("conf/smsconfig.properties", "weina.charset", weinacharset);
		out.println("<br>完成维那短信设置！");		
	}
	
	if ("YouShang".equals(provider)) {
		String youshangaccount = request.getParameter("youshang.account");
		if (youshangaccount == null) youshangaccount = "test";
		String youshangpassword = request.getParameter("youshang.password");
		if (youshangpassword == null) youshangpassword = "test";
		String youshanghost = request.getParameter("youshang.host");
		if (youshanghost == null) youshanghost = "http://api.youshang.com";
					
		PropertyFile.writeProperties("conf/smsconfig.properties", "youshang.account", youshangaccount);
		PropertyFile.writeProperties("conf/smsconfig.properties", "youshang.password", youshangpassword);
		PropertyFile.writeProperties("conf/smsconfig.properties", "youshang.host", youshanghost);
		out.println("<br>完成金蝶友商短信设置！");		
	}
	
%>
<BR>
设置成功后，需要重新启动服务器！<a href="mobile.jsp">返回设置页面</a>
			</td>
		</tr>
	</table>
</body>
</html>
